<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\blog\model;
use think\Model;
use think\db;

class CouModel extends Model
{
    public  function session()
    {
        $id=cmf_get_current_admin_id();
        $where['agency_id']=$id;
        $session=DB::name('shop')->where($where)->find();
        return $session;
    }
    /*优惠券设置列表*/
    public function index($cause)
    {
        $session=$this->session();
        $where['shop_id']=$session['id'];
        $data=DB::name('cou')->where($where)->paginate(10);
        if($data->toArray())
        {
            foreach ($data as $k=>$v)
            {
                $data->toArray();
                $date=array();
                $date=$v;
                if($v['status']==1){
                    $date['status_name']='开启';
                }else{
                    $date['status_name']='关闭';
                }
                if($v['scope']==1)
                {
                    $date['scope_name']='通用';
                }
                $date['con']='满'.$date['cause'].'减'.$date['money'];
                $data->offsetSet($k,$date);
            }
        }

        return $data;

    }
    /*添加优惠券*/
    public function add($array)
    {
        $session=$this->session();
        $start=strtotime($array['s_time']);
        $end=strtotime($array['e_time']);
        if($end>$start)
        {
            $data['cou_guid']=guid();
            $data['cause']=$array['cause'];
            $data['money']=$array['money'];
            $data['s_time']=$start;
            $data['e_time']=$end;
            $data['shop_id']=$session['id'];
            $state=DB::name('cou')->insert($data);
        }else{
            $state=0;
        }
        return $state;
    }

   /*修改优惠券*/
   public function detail($where,$array)
   {
       $session=$this->session();
       $where['shop_id']=$session['id'];
       $status=DB::name('cou')->where($where)->update($array);
       return $status;
   }
   /*
    * 删除
    * 先判断是否有该类别的商品 然后看 是否有该类别的订单
    * */
   public function del($cause)
   {
       $session=$this->session();
       $where['shop_id']=$session['id'];
       $where['voucher_id']=$cause['id'];
       $coupon=DB::name('coupon')->where($where)->find();
       if($coupon)
       {
           $status['num']=0;
           $status['con']='该优惠券在使用中，无法删除';
       }else{
           $why['shop_id']=$session['id'];
           $why['id']=$cause['id'];
           $cou=DB::name('cou')->delete($why);
           if($cou)
           {
               $status['num']=1;
               $status['con']='删除成功';
           }else{
               $status['num']=0;
               $status['con']='删除失败';
           }
       }
       return $status;
   }
   /**
    * 根据id获取列表记录
   */
   public function getcone($where){
       $session=$this->session();
       $where['shop_id']=$session['id'];
       $status= DB::name('cou')->where($where)->find();
       return $status;
   }

}